package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyh.entity.*;
import com.zyh.service.ISkuInfoService;
import com.zyh.service.ISpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * spu信息 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/spuInfo")
public class SpuInfoController {
    @Autowired
    private ISpuInfoService spuInfoService;
    @Autowired
    private ISkuInfoService skuInfoService;

    //根据分类的id查询该分类下面的spu信息，分页查询
    @GetMapping("/selectSpuByCategoryId/{categoryId}")
    public ResultEntity<IPage<SpuInfo>> selectSpuByCategoryId(@PathVariable("categoryId") Integer categoryId, @RequestBody Page page){
        IPage<SpuInfo> spuInfoIPage = null;
        int category_id = spuInfoService.count(new QueryWrapper<SpuInfo>().eq("category_id", categoryId));
        if(category_id>0){
            List<Object> objects = spuInfoService.listObjs(new QueryWrapper<SpuInfo>()
                    .eq("category_id", categoryId)
                    .select("id"));
            spuInfoIPage = spuInfoService.page(page, new QueryWrapper<SpuInfo>().in("id", objects));
        }
        return ResultEntity.ok(spuInfoIPage);
        //return ResultEntity.ok(spuInfoService.list(new QueryWrapper<SpuInfo>().eq("category_id",categoryId)));
    }

    //添加Spu
    @PostMapping("/add")
    public ResultEntity add(@RequestBody SpuInfo spuInfo){
        return ResultEntity.ok(spuInfoService.save(spuInfo));
    }

    //根据spu的id查询该分类下面的sku信息
    @GetMapping("/selectSkusBySpuId/{spuId}")
    public ResultEntity<List<SkuInfo>> selectSkusBySpuId(@PathVariable("spuId") Integer spuId){
        return ResultEntity.ok(skuInfoService.list(new QueryWrapper<SkuInfo>().eq("spu_id",spuId)));
    }

}

